﻿@{
    ViewBag.Title = "User Country Site Management";
}
<br />
<h2>User Country Site Management</h2>
<br />

@(Html.Telerik().Grid<Century.Model.UserProfile>()
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.UserId))
    .ToolBar(c =>
    {
        c.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" });
    })
    .Columns(columns =>
    {

        columns.Bound(o => o.UserId).Hidden().ReadOnly(true);
        columns.Bound(o => o.UserName);
        columns.Bound(o => o.FirstName);
        columns.Bound(o => o.LastName);
        columns.Bound(o => o.IsAdministrator);
        columns.Command(command => { command.Edit().ButtonType(GridButtonType.Image); }).Width(50);
    })
    //.DetailView(details => details.ClientTemplate(
    //    Html.Telerik().Grid<Century.Model.CMS.Site>()
    //        .DataKeys(keys => keys.Add(c => c.Id))
    //        .Name("CountrySite_<#= Id #>")
    //        .Columns(columns =>
    //        {
    //            columns.Bound(o => o.Id);
    //            columns.Bound(o => o.Name);
    //            columns.Bound(o => o.Path);
    //            columns.Bound(o => o.UserHasThisCountrySite)
    //            .ClientTemplate("<input type='checkbox' name='UserHasThisCountrySite' value='<#= Id #>'  <#= UserHasThisCountrySite?'checked':'' #> onchange = 'ChangeUserCountries(this)' />");
    //        })
    //        .DataBinding(dataBinding => dataBinding.Ajax()
    //                          .Select("GetCountriesForUser", "UserSiteManagement", new { userId = "<#= Id #>" })
    //                    )
    //                    .Sortable()
    //                    .Filterable()
    //                    .ToHtmlString()
    // ))
            .DataBinding(dataBinding => dataBinding.Ajax()
            .Insert("AddUser", "UserRoleManagement")
            .Select("GetUserList", "UserRoleManagement")
            .Update("EditUser", "UserRoleManagement")
        )
    .Groupable()
    .Filterable()
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Sortable(sortable => sortable.OrderBy(order => order.Add(o => o.UserName)))
    .Pageable(pagingSetting => pagingSetting.PageSize(20))
    .Resizable(config => { config.Columns(true); })
    .Reorderable(config => { config.Columns(true); })
    .ColumnContextMenu()
)
<script type="text/javascript">
    function ChangeUserCountries(sender) {
        $.ajax({
            type: "post",
            url: '@Url.Action("ChangeUserCountries", "UserSiteManagement")',
            data: { userId: sender.parentElement.parentElement.parentElement.parentElement.parentElement.id.split('_')[1],
                countrySiteId: sender.value,
                userHasThisCountry: sender.checked
            },
            dataType: "html",
            success: function (msg) {
                alert('User country site has been changed successfully');
            },
            error: function (msg) {
                alert('Error on changin user country site');
            }
        });
    }

   
   
</script>
